﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;


namespace APPWG4806
{
  public class HttpHelp
    {
        
        #region  //CURD
        
        //创建
        public  static  string PostData(string url, object data)
        {
            HttpWebResponse httpWebResponse = null;
            StreamReader streamReader = null;
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.Proxy = null;
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Accept = "application/json";
                httpWebRequest.Method = "POST";
                httpWebRequest.Timeout = 30000;
                byte[] btBodys = Encoding.UTF8.GetBytes(data.ToString());
                httpWebRequest.ContentLength = btBodys.Length;
                httpWebRequest.ServicePoint.Expect100Continue = false;
                httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                string responseContent = streamReader.ReadToEnd();
                return responseContent;
            }
            catch (Exception ex)
            {

                return ex.ToString();
            }
        }


        //修改
        public static  string PutData(string url, dynamic id, object data)
        {
            HttpWebResponse httpWebResponse = null;
            StreamReader streamReader = null;
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url + "/" + id);
                httpWebRequest.Proxy = null;
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Accept = "application/json";
                httpWebRequest.Method = "PUT";
                httpWebRequest.Timeout = 30000;
                byte[] btBodys = Encoding.UTF8.GetBytes(data.ToString());
                httpWebRequest.ContentLength = btBodys.Length;
                httpWebRequest.ServicePoint.Expect100Continue = false;
                httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                string responseContent = streamReader.ReadToEnd();
                return responseContent;

            }
            catch (Exception ex)
            {

                return ex.ToString();
            }
        }


        //查询
        public static  string GetData(string url, string author)
        {
            try
            {
                string responseText;
                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";

                if (!string.IsNullOrEmpty(author))
                {
                    request.Headers["Authorization"] = author;
                }

                using (HttpWebResponse hwr = request.GetResponse() as HttpWebResponse)
                {
                    using (System.IO.StreamReader myreader = new System.IO.StreamReader(hwr.GetResponseStream(), Encoding.UTF8))
                    {
                        responseText = myreader.ReadToEnd();
                    }
                }
                return responseText;
            }
            catch (Exception)
            {
                throw;
            }
        }


        //删除
        public static string DeleteData(string url, dynamic id)
        {
            string result = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "/" + id);
                request.Method = "DELETE";
                request.ServicePoint.Expect100Continue = false;
                WebResponse response = (WebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                result = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                result = ex.ToString();
            }
            return result;
        }
        #endregion
    }
}
